/*
 * @Descripttion: 出手鸭小程序
 * @version:1.0.1 
 * @Author: HHL
 * @Date: 2023-10-15 10:15:47
 * @LastEditTime: 2023-11-08 22:10:11
 * @LastEditors: sueRimn
 */


import {
  fetchProgramLogin,
} from '@/api'

// 获取登录code

const getLoginCode = ({ success, fail }) => {
  // #ifdef MP-ALIPAY
  my.getAuthCode({
    scopes: ['auth_base'],
    success: ({ authCode }) => {
      success(authCode)
    },
    fail: res => {
      fail(res)
    }
  })
  // #endif
  // #ifdef MP-WEIXIN
  uni.login({
    provider: 'weixin',
    success: ({ code }) => {
      success(code)
    },
    fail: res => {
      fail(res)
    }
  })
  // #endif
// #ifdef MP-XHS
console.log("===========>","当前小红书环境")
  uni.login({
    success: res => {
      console.log("========>",res)
      if(res.code) {
        success(res.code)
      }
    },fail:error => {
      console.log("========>",error)
    }
  })
  // #endif
}

const loadOpenid = () => {
  return new Promise(resolve => {
    getLoginCode({
      success: code => {
        fetchProgramLogin({ code }).then(({ data, code }) => {
          if (code === 200) {
            resolve({ code, data })
          } else {
            resolve(false)
          }
        })
      }
    })

  })
}

export default loadOpenid